<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>菜单管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    菜单筛选
                </div>
                <div class="layui-inline">
                    <select id="parentid" lay-filter="parentid">
                        <option value="-1">全部</option>
                        <option value="0">一级菜单</option>
                        <option value="1">二级菜单</option>
                    </select>
                </div>
                <!-- 搜索按钮 -->
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-list" id="menu_search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>

                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-role" data-type="add">添加</button>
                </div>
            </div>
        </div>
        <div class="layui-card-body">
            <table id="menuTable" lay-filter="menuTable"></table>
        </div>
    </div>
</div>

<script type="text/html" id="table_bar">
    <div class="layui-btn-container">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </div>
</script>

<script src="../../js/jquery-1.8.3.min.js"></script>
<script src="../../layuiadmin/layui/layui.js"></script>
<script>
    //用于选择页面
    var menuType = "";
    var menuURL = "";
    var menuId = "";
    //layui配置
    layui.config({
        base: '../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'useradmin', 'table'], function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table
            , layer = layui.layer;

        //监听搜索
        $("#menu_search").on('click', function (data) {
                var parentid = $('#parentid').val();
                //执行重载
                table.reload('menuTable', {
                    method: 'post'
                    , where: {'parentid': parentid}
                    , page: {curr: 1}
                });
            }
        );

        //表格执行渲染
        table.render({
            elem: '#menuTable'
            , id: 'menuTable'
            , url: '/menu/findPage'
            , page: true
            , limits: [10, 20, 30, 50]
            , cols: [[
                {
                    field: 'parentname', title: '父级菜单', templet: function (d) {
                        if (typeof (d.parentname) == "undefined") return "顶级菜单";
                        else return d.parentname;
                    }
                }
                , {field: 'name', title: '菜单名称'}
                , {field: 'url', title: '路径(url)'}
                , {field: 'icon', title: '图标路径'}
                , {fixed: 'right', title: '操作', align: 'center', toolbar: '#table_bar'}
            ]]
            , where: {
                parentid: $("#parentid").val()
            }
        });

        //添加操作
        var $ = layui.$, active = {
            add: function () {
                menuType = "add";
                menuURL = "/menu/add";
                layer.open({
                    type: 2
                    , title: '添加菜单信息'
                    , content: 'menuform.html'
                    , maxmin: true
                    , area: ['400px', '500px']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#menu-submit");
                        submit.click();
                    }

                });
            }
        };

        //监听行工具事件
        table.on('tool(menuTable)', function (obj) { //注：tool 是工具条事件名，zq_table 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象
            switch (layEvent) {
                case 'del':
                    layer.confirm('您确定删除id：' + data.id + '的数据吗？', function (index) {
                        //向服务端发送删除指令，在这里可以使用Ajax异步
                        $.post("/menu/delete", {id: data.id}, function (ret) {
                            layer.msg(ret.msg, {icon: ret.icon, time: 1500});
                            if (ret.status == "1") {//删除成功，刷新当前页表格
                                obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                                layer.close(index);
                                $(".layui-laypage-btn").click();//点击分页刷新当前页
                            }
                        });
                    });
                    break;
                case 'edit':
                    menuType = "toUpdate";
                    menuURL = "/menu/update";
                    menuId = data.id;
                    layer.open({
                        type: 2
                        , title: '修改菜单信息'
                        , content: 'menuform.html'
                        , maxmin: true
                        , area: ['400px', '500px']
                        , btn: ['确定', '取消']
                        , yes: function (index, layero) {
                            //点击确认触发 iframe 内容中的按钮提交
                            var submit = layero.find('iframe').contents().find("#menu-submit");
                            submit.click();
                        }

                    });
                    break;
            }
        });
        //必备
        $('.layui-btn.layuiadmin-btn-role').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>

